package com.didibigdata.Repositories;

import com.didibigdata.entities.OrderNum;
import com.didibigdata.utils.JDBCTools;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lkabb on 2020/6/7
 */
public class OrderNumRepository {

    public List<OrderNum> getOrderNumList(int selectTime) {
        //日志
        Logger logger = LoggerFactory.getLogger(OrderNumRepository.class);
        logger.info("add OrderNumRepository logger successful!");

        ResultSet resultSetmysql = null;
        Connection connectionmysql = null;
        PreparedStatement statementmysql = null;
        List<OrderNum> orderNumList = new ArrayList<OrderNum>();
        try {
            connectionmysql = JDBCTools.getConnection();
            String sqltime = "select * from t_didi" + selectTime + "ordercount";
            statementmysql = connectionmysql.prepareStatement(sqltime);
            logger.info("connect to JDBC and executeQuery [{}]", sqltime);
            resultSetmysql = statementmysql.executeQuery();
            while (resultSetmysql.next()) {
                OrderNum orderNum = new OrderNum();
                orderNum.setDay(resultSetmysql.getString(1));
                orderNum.setCount(Integer.parseInt(resultSetmysql.getString(2)));
                orderNumList.add(orderNum);
            }
            logger.info("getOrderNumList of [{}] successful!", selectTime);
        } catch (Exception e) {
            logger.error("OrderNumRepository SQLException: [{}]", e);
            e.printStackTrace();
        } finally {
            JDBCTools.releaseConnection(connectionmysql, statementmysql, resultSetmysql);
        }

        return orderNumList;
    }
}
